package com.piggy.sys.controller;

import cn.hutool.core.util.BooleanUtil;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.domain.R;
import com.piggy.common.core.exception.base.BaseException;
import com.piggy.common.core.utils.FeignResultUtils;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.annotation.Inner;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.api.RemoteLogiMenuService;
import com.piggy.logi.sys.api.RemoteLogiRoleService;
import com.piggy.logi.sys.api.bo.LogiSysMenuBo;
import com.piggy.logi.sys.api.domain.LogiSysRole;
import com.piggy.logi.sys.api.domain.LogiSysUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单信息
 *
 * @author shark
 */
@RestController
@RequestMapping("/logi/role")
public class LogiSysRoleController extends BaseController {
    @Resource
    private RemoteLogiRoleService roleService;

    /**
     * 删除角色
     */
    //@SaCheckPermission("system:role:remove")
    @PostMapping("/remove/{roleIds}")
    public R innerRemove(@PathVariable Long[] roleIds) {
        R r = roleService.innerRemove(roleIds, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    @GetMapping("/getRoleByKey/{roleKey}")
    public R<LogiSysRole> getRoleByKey(@PathVariable(value = "roleKey") String roleKey) {
        R<LogiSysRole> r = roleService.getRoleByKey(roleKey, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    @GetMapping("/getRoleUser/{roleId}")
    public R<List<LogiSysUser>> getRoleUsers(@PathVariable(value = "roleId") Long roleId) {
        R<List<LogiSysUser>> r = roleService.getRoleUsers(roleId, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    @PostMapping("/queryRoleAll")
    public R<List<LogiSysRole>> queryRoleAll() {
        R<List<LogiSysRole>> r = roleService.queryRoleAll(0L, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    @GetMapping("/getById/{roleId}")
    public R<LogiSysRole> getById(@PathVariable(value = "roleId") Long roleId) {
        R<LogiSysRole> r = roleService.getById(roleId, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 新增角色
     */
    //@SaCheckPermission("system:role:add")
    @PostMapping("/add")
    public R innerAdd(@Validated @RequestBody LogiSysRole role) {
        role.setDeptId(0L);
        R r = roleService.innerAdd(role, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 修改保存角色
     */
    //@SaCheckPermission("system:role:edit")
    @PutMapping("/edit")
    public R innerEdit(@Validated @RequestBody LogiSysRole role) {
        role.setDeptId(0L);
        R r = roleService.innerEdit(role, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 修改保存数据权限
     */
    //@SaCheckPermission("system:role:edit")
    @PutMapping("/dataScope")
    public R innerDataScope(@RequestBody LogiSysRole role) {
        role.setDeptId(0L);
        R r = roleService.innerDataScope(role, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 状态修改
     */
    //@SaCheckPermission("system:role:edit")
    @PutMapping("/changeStatus")
    public R innerChangeStatus(@RequestBody LogiSysRole role) {
        role.setDeptId(0L);
        R r = roleService.innerChangeStatus(role, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

}
